
<?php

/**
 * @Author:      zwh
 * @DateTime:    2016-7-27 11:04:08
 * @Description: 订单退货
 */
class ReturnOrder extends Eloquent
{
    protected $table = "return_order";
    
    public static $reason = array(
        "0"=>"无理由退货",
        "1"=>"质量存疑",
        "2"=>"折扣存疑",
        "3"=>"政府",
    );
    public static $accredit_type = array(
        "0"=>"账号授权",
        "1"=>"动态授权",
    );
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-7-27 14:56:13
     * @Description: 配件列表
     */
    public static function  getItemsByOrderId($order_id){
        $or=ReturnOrder::where('order_id',$order_id)->whereIn('status',array(0,1))->first();
        $all1=$all=$allItem=$items=$promotion_prouduct=$base_product=$is=array();
        $order_product=OrderProduct::where("order_id",$order_id)->where("status",1)->get()->toArray();
        foreach ($order_product as $_op) {
            $sum=ReturnOrderItem::where('return_order_id',$or->id)->where('order_product_id',$_op['id'])->where('type',4)->whereIn('status',array(0,1))->first();
            if($sum){//如果该订单产品包退款
                continue;
            }
            switch ($_op['product_type']) {
                case 'enterprise':
                    $opp_ids=OrderProductEsPackage::where("order_product_id",$_op['id'])->select('id')->get()->toArray();
                    $opp_ids = array_column($opp_ids, 'id');
                    $opei=OrderProductEsItem::where('type',1)->whereIn('order_product_package_id',$opp_ids)->get();
                    foreach ($opei as $key=>$_item) {
                        $opep=OrderProductEsPackage::find($_item->order_product_package_id);
                        // if($_item->item_type==0){
                            $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                            $_item->scf_name=$item_info->scf_name;
                            $_item->subject=$item_info->subject;
                            $_item->unit=$item_info->unit;
                            $_item->brand_name=$item_info->brand_name;
                            $_item->series_name=$item_info->series_name;
                            $_item->model=$item_info->model;
                            $_item->specification=$item_info->specification;
                        // }else{
                        //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
                        //     $_item->scf_name=$item_info->scf_name;
                        //     $_item->subject=$item_info->subject;
                        //     $_item->unit=$item_info->unit;
                        //     $_item->brand_name=$item_info->brand_name;
                        //     $_item->series_name=$item_info->series_name;
                        //     $_item->model=$item_info->model;
                        //     $_item->specification=$item_info->specification;
                        // }
                        $_item->product_type='enterprise';
                        $_item->package_name=$opep->package_name;
                        $cost=ReturnOrderItem::getCost($_item->id,$_op['product_type'],$order_id);
                        $sale_cost=$_item->num*$_item->counter_amount;//配件销售总额
                        if($sale_cost-$cost>0){
                            $_item->allow_return= round($sale_cost-$cost);//配件毛利
                            $allItem[]=$_item;
                        }
                        $items[]=$_item;
                    }
                    break;
                case 'activity':
                    $opp_ids=OrderProductAcPackage::where("order_product_id",$_op['id'])->select('id')->get()->toArray();
                    $opp_ids = array_column($opp_ids, 'id');
                    $opaci=OrderProductAcItem::where('type',1)->whereIn('order_product_package_id',$opp_ids)->get();
                    foreach ($opaci as $key=>$_item) {
                        $opacp=OrderProductAcPackage::find($_item->order_product_package_id);
                        // if($_item->item_type==0){
                            $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                            $_item->scf_name=$item_info->scf_name;
                            $_item->subject=$item_info->subject;
                            $_item->unit=$item_info->unit;
                            $_item->brand_name=$item_info->brand_name;
                            $_item->series_name=$item_info->series_name;
                            $_item->model=$item_info->model;
                            $_item->specification=$item_info->specification;
                        // }else{
                        //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
                        //     $_item->scf_name=$item_info->scf_name;
                        //     $_item->subject=$item_info->subject;
                        //     $_item->unit=$item_info->unit;
                        //     $_item->brand_name=$item_info->brand_name;
                        //     $_item->series_name=$item_info->series_name;
                        //     $_item->model=$item_info->model;
                        //     $_item->specification=$item_info->specification;
                        // }
                        $_item->product_type='activity';
                        $_item->package_name=$opacp->package_name;
                        $cost=ReturnOrderItem::getCost($_item->id,$_op['product_type'],$order_id);
                        $sale_cost=$_item->num*$_item->counter_amount;//配件销售总额
                        if($sale_cost-$cost>0){
                            $_item->allow_return= round($sale_cost-$cost);//配件毛利
                            $allItem[]=$_item;
                        }
                        $items[]=$_item;
                    }
                    break;
                case 'promotion':
                    $opp_ids=OrderProductPackage::where("order_product_id",$_op['id'])->select('id')->get()->toArray();
                    $opp_ids = array_column($opp_ids, 'id');
                    $items=OrderProductItem::where('type',1)->whereIn('order_product_package_id',$opp_ids)->get()->toArray();
                    foreach ($items as $key=>$_item) {
                        $opp=OrderProductPackage::find($_item->order_product_package_id);
                        $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                        $_item->scf_name=$item_info->scf_name;
                        $_item->unit=$item_info->unit;
                        $_item->brand_name=$item_info->brand_name;
                        $_item->series_name=$item_info->series_name;
                        $_item->model=$item_info->model;
                        $_item->specification=$item_info->specification;
                        $_item->product_type='promotion';
                        $_item->package_name=$opp->package_name;
                        $cost=ReturnOrderItem::getCost($_item->id,$_op['product_type'],$order_id);
                        $sale_cost=$_item->num*$_item->counter_amount;//配件销售总额
                        if($sale_cost-$cost>0){
                            $_item->allow_return=round($sale_cost-$cost);//配件毛利
                            $allItem[]=$_item;
                        }
                        $items[]=$_item;
                    }
                    break;
                case 'base':
                    $opp=OrderProductBaseItem::where("order_product_package_id",$_op['id'])
                    ->where('type',1)
                    ->get();
                    foreach ($opp as $key=>$_item) {
                        // if($_item['item_type']==0){
                            $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                            $_item->scf_name=$item_info->scf_name;
                            // $_item->subject_name=$item_info->subject_name;
                            $_item->subject=$item_info->subject;
                            $_item->brand_name=$item_info->brand_name;
                            $_item->series_name=$item_info->series_name;
                            $_item->model=$item_info->model;
                            $_item->specification=$item_info->specification;
                        // }else{
                        //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
                        //     $_item->scf_name=$item_info->scf_name;
                        //     // $_item->subject_name="";
                        //     $_item->subject=$item_info->subject;
                        //     $_item->brand_name=$item_info->brand_name;
                        //     $_item->series_name=$item_info->series_name;
                        //     $_item->model=$item_info->model;
                        //     $_item->specification=$item_info->specification;
                        // }
                        $_item->product_type='base';
                        $_item->package_name=$_op['product_name'];
                        $cost=ReturnOrderItem::getCost($_item->id,$_op['product_type'],$order_id);
                        $sale_cost=$_item->num*$_item->counter_amount;//配件销售总额
                        if($sale_cost-$cost>0){
                            $_item->allow_return=round($sale_cost-$cost);//配件毛利
                            $allItem[]=$_item;
                        }
                        $items[]=$_item;
                    }
                    break;
                case 'non_standard':
                    $opn=OrderProductNonStandard::where("order_product_id",$_op['id'])
                    ->where('service_type',1)
                    ->where('order_product_non_standard.status',1)
                    ->get();
                    foreach ($opn as $key=>$_item) {
                        //判断产品项是非标还是标准配件
                        // if($_item['type']==0){
                            $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                            $_item->scf_name=$item_info->scf_name;
                            $_item->subject=$item_info->subject;
                            $_item->brand_name=$item_info->brand_name;
                            $_item->series_name=$item_info->series_name;
                            $_item->model=$item_info->model;
                            $_item->specification=$item_info->specification;
                            $_item->item_name=$_item->name;
                        // }else{
    
                        //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
                        //     $_item->scf_name=$item_info->scf_name;
                        //     $_item->subject=$item_info->subject;
                        //     $_item->brand_name=$item_info->brand_name;
                        //     $_item->series_name=$item_info->series_name;
                        //     $_item->model=$item_info->model;
                        //     $_item->specification=$item_info->specification;
                        // }
                        $_item->product_type='non_standard';
                        $_item->package_name=$_op['product_name'];
                        $cost=ReturnOrderItem::getCost($_item->id,$_op['product_type'],$order_id);
                        $sale_cost=$_item->num*$_item->counter_amount;//配件销售总额
                        if($sale_cost-$cost>0){
                            $_item->allow_return=round($sale_cost-$cost);//配件毛利
                            $allItem[]=$_item;
                        }
                        $items[]=$_item;
                    }
                    break;
                case 'service_card':
                    $opp=OrderProductScItem::where("order_product_id",$_op['id'])
                    ->where('type',1)
                    ->get();
                    foreach ($opp as $key=>$_item) {
                        // if($_item['item_type']==0){
                            $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                            $_item->scf_name=$item_info->scf_name;
                            // $_item->subject_name=$item_info->subject_name;
                            $_item->subject=$item_info->subject;
                            $_item->brand_name=$item_info->brand_name;
                            $_item->series_name=$item_info->series_name;
                            $_item->model=$item_info->model;
                            $_item->specification=$item_info->specification;
                        // }else{
                        //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
                        //     $_item->scf_name=$item_info->scf_name;
                        //     // $_item->subject_name="";
                        //     $_item->subject=$item_info->subject;
                        //     $_item->brand_name=$item_info->brand_name;
                        //     $_item->series_name=$item_info->series_name;
                        //     $_item->model=$item_info->model;
                        //     $_item->specification=$item_info->specification;
                        // }
                        $_item->product_type='service_card';
                        $_item->package_name=$_op['product_name'];
                        $cost=ReturnOrderItem::getCost($_item->id,$_op['product_type'],$order_id);
                        $sale_cost=$_item->num*$_item->counter_amount;//配件销售总额
                        if($sale_cost-$cost>0){
                            $_item->allow_return=round($sale_cost-$cost);//配件毛利
                            $allItem[]=$_item;
                        }
                        $items[]=$_item;
                    }
                    break;
            }
        }
        foreach($allItem  as $k1=>$val1){
            $updated_at1[$k1]=$val1['updated_at'];
        }
        if($allItem)
            array_multisort($updated_at1, SORT_DESC, $allItem);
        
        
           
        //如果某个配件已退完则列表将不再显示
        foreach($allItem as $key=>$item1){            
            $sum1=ReturnOrderItem::where('return_order_id',$or->id)->whereIn('status',array(0,1))->where('order_product_item_id',$item1->id)->where('product_type',$item1->product_type)->sum('return_amount');
            
            if($sum1<$item1->num){
                $item1->allow_return-=$sum1;
                $all[]=$item1;
            }
        }
        foreach($items  as $k1=>$val1){
            $updated_at1[$k1]=$val1['updated_at'];
        }
        if($items)
            array_multisort($updated_at1, SORT_DESC, $items);
         
        //如果某个配件已退完则列表将不再显示
        foreach($items as $key=>$item2){
            // $or=ReturnOrder::where('order_id',$order_id)->whereIn('status',array(0,1))->first();
            $sum=ReturnOrderItem::where('return_order_id',$or->id)->whereIn('status',array(0,1))->where('order_product_item_id',$item2->id)->where('product_type',$item2->product_type)->sum('number');
            $a[]=$item2->num;
            
            if($sum<$item2->num){
                $item2->num-=$sum;
                $all1[]=$item2;
            }
        }
        return array('item1'=>$all,'item2'=>$all1);
    }

    /**
     * @Author:      lyt
     * @DateTime:  2017-08-04 09:36:49
     * @Description: 用于获取未被退货占用的订单产品
     */
    public static function  getProductByOrderId($order_id){
        $order_product=OrderProduct::where("order_id",$order_id)->where("status",1)->get()->toArray();
        $or=ReturnOrder::where('order_id',$order_id)->whereIn('status',array(0,1))->first();//此接口在退货单调 必然有该对象
        $finally=array();
        //遍历简餐每个产品包内的耗材是否有退货，  后面可以用退款详情的订单产品字段判断 ，暂时存在历史数据没有产品字段暂时这样判断。
        foreach ($order_product as $_op) {           
            $sum=ReturnOrderItem::where('return_order_id',$or->id)->where('order_product_id',$_op['id'])->where('type',4)->whereIn('status',array(0,1))->first();
            if($sum){//如果该订单产品包退款
                continue;
            }
            switch ($_op['product_type']) {
                case 'enterprise':
                    $opp_ids=OrderProductEsPackage::where("order_product_id",$_op['id'])->select('id')->get()->toArray();
                    $opp_ids = array_column($opp_ids, 'id');
                    $opei=OrderProductEsItem::whereIn('order_product_package_id',$opp_ids)->get();
                    foreach ($opei as $key=>$_item) { //查看是否 该包中已经被添加过 退货
                        $f=ReturnOrderItem::where('order_product_item_id',$_item->id)->where('product_type',"enterprise")->where('return_order_id',$or->id)->whereIn('status',array(0,1))->first();
                        if($f)break 2;
                    }
                    $finally[]=$_op;
                    break;
                case 'activity':
                    $opp_ids=OrderProductAcPackage::where("order_product_id",$_op['id'])->select('id')->get()->toArray();
                    $opp_ids = array_column($opp_ids, 'id');
                    $opaci=OrderProductAcItem::whereIn('order_product_package_id',$opp_ids)->get();
                    foreach ($opaci as $key=>$_item) {
                        $f=ReturnOrderItem::where('order_product_item_id',$_item->id)->where('product_type',"activity")->where('return_order_id',$or->id)->whereIn('status',array(0,1))->first();
                        if($f)break 2;
                    }
                    $finally[]=$_op;
                    break;
                case 'promotion':
                    $opp_ids=OrderProductPackage::where("order_product_id",$_op['id'])->select('id')->get()->toArray();
                    $opp_ids = array_column($opp_ids, 'id');
                    $items=OrderProductItem::whereIn('order_product_package_id',$opp_ids)->get()->toArray();
                    foreach ($items as $key=>$_item) {
                        $f=ReturnOrderItem::where('order_product_item_id',$_item->id)->where('product_type',"promotion")->where('return_order_id',$or->id)->whereIn('status',array(0,1))->first();
                        if($f)break 2;
                    }
                    $finally[]=$_op;
                    break;
                case 'base':
                    $opp=OrderProductBaseItem::where("order_product_package_id",$_op['id'])
                    ->get();
                    foreach ($opp as $key=>$_item) {
                        $f=ReturnOrderItem::where('order_product_item_id',$_item->id)->where('product_type',"base")->where('return_order_id',$or->id)->whereIn('status',array(0,1))->first();
                        if($f)break 2;
                    }
                    $finally[]=$_op;
                    break;
                case 'non_standard':
                    $opn=OrderProductNonStandard::where("order_product_id",$_op['id'])
                    ->where('order_product_non_standard.status',1)
                    ->get();
                    foreach ($opn as $key=>$_item) {
                        //判断产品项是非标还是标准配件
                        $f=ReturnOrderItem::where('order_product_item_id',$_item->id)->where('product_type',"non_standard")->where('return_order_id',$or->id)->whereIn('status',array(0,1))->first();
                        if($f)break 2;
                    }
                    $finally[]=$_op;
                    break;
                case 'service_card':
                    $opp=OrderProductScItem::where("order_product_id",$_op['id'])
                    ->get();
                    foreach ($opp as $key=>$_item) {
                        $f=ReturnOrderItem::where('order_product_item_id',$_item->id)->where('product_type',"service_card")->where('return_order_id',$or->id)->whereIn('status',array(0,1))->first();
                        if($f)break 2;
                    }
                    $finally[]=$_op;
                    break;
            }
        }        
        return $finally;
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-7-27 14:56:13
     * @Description: 工时列表
     */
    public static function  getHoursByOrderId($order_id){
    
        $all=$allHours=$is=array();
        $order_product=OrderProduct::where("order_id",$order_id)->where("status",1)->get()->toArray();
        $or=ReturnOrder::where('order_id',$order_id)->whereIn('status',array(0,1))->first();//此接口在退货单调 必然有该对象
        foreach ($order_product as $_op) {
            // $sum=ReturnOrderItem::where('order_product_id',$_op['id'])->where('type',4)->whereIn('status',array(0,1))->first();
            $sum=ReturnOrderItem::where('return_order_id',$or->id)->where('order_product_id',$_op['id'])->where('type',4)->whereIn('status',array(0,1))->first();
            if($sum){//如果该订单产品包退款
                continue;
            }
            switch ($_op['product_type']) {
                case 'enterprise':
                    
                    $opp_ids=OrderProductEsPackage::where("order_product_id",$_op['id'])->select('id')->get()->toArray();
                    $opp_ids = array_column($opp_ids, 'id');
                    
                    $opei=OrderProductEsItem::where('type',2)->whereIn('order_product_package_id',$opp_ids)->get();
                    
                    foreach ($opei as $key=>$_item) {
                        $opep=OrderProductEsPackage::find($_item->order_product_package_id);
                        $_item->product_type='enterprise';
                        $_item->order_product_id=$_op['id'];
                        $_item->package_name=$opep->package_name;
                        $_item->allow_return=round($_item->counter_amount);
                        $allHours[]=$_item;
                    }
                    break;
                case 'activity':
                
                    $opp_ids=OrderProductAcPackage::where("order_product_id",$_op['id'])->select('id')->get()->toArray();
                    $opp_ids = array_column($opp_ids, 'id');
                
                    $opaci=OrderProductAcItem::where('type',2)->whereIn('order_product_package_id',$opp_ids)->get();
                
                    foreach ($opaci as $key=>$_item) {
                        $opacp=OrderProductAcPackage::find($_item->order_product_package_id);
                        $_item->product_type='activity';
                        $_item->order_product_id=$_op['id'];
                        $_item->package_name=$opacp->package_name;
                        $_item->allow_return=round($_item->counter_amount);
                        $allHours[]=$_item;
                    }
                    break;
                case 'promotion':
                    $opp_ids=OrderProductPackage::where("order_product_id",$_op['id'])->select('id')->get()->toArray();
                    $opp_ids = array_column($opp_ids, 'id');
                    $items=OrderProductItem::where('type',2)->whereIn('order_product_package_id',$opp_ids)->get()->toArray();
                    foreach ($items as $key=>$_item) {
                        $opp=OrderProductPackage::find($_item->order_product_package_id);
                        $_item->product_type='promotion';
                        $_item->order_product_id=$_op['id'];
                        $_item->package_name=$opep->package_name;
                        $_item->allow_return=round($_item->counter_amount);
                        $allHours[]=$_item;
                    }
                    break;
                case 'base':
                    $opp=OrderProductBaseItem::where("order_product_package_id",$_op['id'])
                    ->where('type',2)
                    ->get();
                    foreach ($opp as $key=>$_item) {
                        $_item->product_type='base';
                        $_item->order_product_id=$_op['id'];
                        $_item->package_name=$_op['product_name'];
                        $_item->allow_return=round($_item->counter_amount);
                        $allHours[]=$_item;
                    }
                    break;
                case 'non_standard':
                    $opn=OrderProductNonStandard::where("order_product_id",$_op['id'])
                    ->where('service_type',2)
                    ->where('order_product_non_standard.status',1)
                    ->get();
                    foreach ($opn as $key=>$_item) {
                        $_item->product_type='non_standard';
                        $_item->order_product_id=$_op['id'];
                        $_item->package_name=$_op['product_name'];
                        $_item->allow_return=round($_item->counter_amount);
                        $_item->item_name=$_item->name;
                        $allHours[]=$_item;
                    }
                    break;
                case 'service_card':
                    $opp=OrderProductScItem::where("order_product_id",$_op['id'])
                    ->where('type',2)
                    ->get();
                    foreach ($opp as $key=>$_item) {
                        $_item->product_type='service_card';
                        $_item->order_product_id=$_op['id'];
                        $_item->package_name=$_op['product_name'];
                        $_item->allow_return=round($_item->counter_amount);
                        $allHours[]=$_item;
                    }
                    break;
            }
        }
        foreach($allHours  as $k1=>$val1){
            $updated_at1[$k1]=$val1['updated_at'];
        }

        if($allHours)
            array_multisort($updated_at1, SORT_DESC, $allHours);
        
        //如果某个配件已退完则列表将不再显示
        foreach($allHours as $key=>$item){
            $or=ReturnOrder::where('order_id',$order_id)->first();
            $sum=ReturnOrderItem::where('return_order_id',$or->id)->whereIn('status',array(0,1))->where('order_product_item_id',$item->id)->where('product_type',$item->product_type)->sum('return_amount');
            if($sum<$item->counter_amount){
                $item->allow_return-=$sum;
                $all[]=$item;
            }
        }
        return $all;
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-7-27 14:56:13
     * @Description: 配件列表
     */
    /**
     * @Author:      lyt
     * @DateTime:  2017-08-03 18:17:16
     * @Description: 本方法用来 折扣均摊 均摊 价格 套擦卡订单 0元故不存于均摊
     */
    public static function  getAllByOrderId($order_id){
        $allItem=$promotion_prouduct=$base_product=$item=$is=array();
        $order_product=OrderProduct::where("order_id",$order_id)->where("status",1)->get()->toArray();
        foreach ($order_product as $_op) {
            switch ($_op['product_type']) {
                case 'enterprise':
                    $opp_ids=OrderProductEsPackage::where("order_product_id",$_op['id'])->select('id')->get()->toArray();
                    $opp_ids = array_column($opp_ids, 'id');
                    $opei=OrderProductEsItem::whereIn('order_product_package_id',$opp_ids)->get();
                    foreach ($opei as $key=>$_item) {
                        $_item->product_type='enterprise';
                        $allItem[]=$_item;
                    }
                    break;
                case 'activity':
                    $opp_ids=OrderProductAcPackage::where("order_product_id",$_op['id'])->select('id')->get()->toArray();
                    $opp_ids = array_column($opp_ids, 'id');
                    $opaci=OrderProductAcItem::whereIn('order_product_package_id',$opp_ids)->get();
                    foreach ($opaci as $key=>$_item) {
                        $_item->product_type='activity';
                        $allItem[]=$_item;
                    }
                    break;
                case 'promotion':
                    $opp_ids=OrderProductPackage::where("order_product_id",$_op['id'])->select('id')->get()->toArray();
                    $opp_ids = array_column($opp_ids, 'id');
                    $items=OrderProductItem::whereIn('order_product_package_id',$opp_ids)->get()->toArray();
                    foreach ($items as $key=>$_item) {
                        $_item->product_type='promotion';
                        $allItem[]=$_item;
                    }
                    break;
                case 'base':
                    $opp=OrderProductBaseItem::where("order_product_package_id",$_op['id'])->get();
                    foreach ($opp as $key=>$_item) {
                        $_item->product_type='base';
                        $allItem[]=$_item;
                    }
                    break;
                case 'non_standard':
                    $opn=OrderProductNonStandard::where("order_product_id",$_op['id'])
                    ->where('order_product_non_standard.status',1)
                    ->get();
                    foreach ($opn as $key=>$_item) {
                        $_item->product_type='non_standard';
                        $allItem[]=$_item;
                    }
            }
        }
        return $allItem;
    }

}